/**
 * 
 */
package com.jm.web.builder.table;

import java.io.IOException;

import com.jm.utils.StringBuilderEx;
import com.jm.web.builder.BaseBuilder;

/**
 * @author yanjie
 * Feb 1, 2012 1:42:05 PM
 */
public class ColumnEditorBuilder extends BaseBuilder{
	@Override
	public void RenderEnd() {
		StringBuilderEx sb = new StringBuilderEx();
		try {
			sb.Append("<script type=\"text/javascript\">");
			
			String type = this.getBaseTag().GetExtValue("type", "");
			StringBuilderEx strSelect = new StringBuilderEx();
			if("text".equals(type) || "calendar".equals(type)){
				strSelect.Append("input[name=%1$s]",this.getBaseTag().getId());
			}else if("textarea".equals(type)){
				strSelect.Append("textarea[name=%1$s]",this.getBaseTag().getId());
			}else if("select".equals(type)){
				strSelect.Append("select[name=%1$s]",this.getBaseTag().getId());
			}else if("combobox".equals(type)){
				strSelect.Append("div[id=%1$s_field]",this.getBaseTag().getId());
			}else if("multiselect".equals(type)){
				strSelect.Append("div[id=%1$s_field]",this.getBaseTag().getId());
			}else{
				strSelect.Append("[name=%1$s]",this.getBaseTag().getId());
			}
			
			sb.Append("$P.grid.editors['%1$s']=$('%2$s');",this.getBaseTag().getId(),strSelect.toString());
//			sb.Append("$P.grid.editors['%1$s']=$('%2$s');",this.getBaseTag().getId(),strSelect.toString());
			
//			sb.Append("\r\n var editor_%1$s=$('%2$s');",this.getBaseTag().getId(),strSelect.toString());
//			
//			if("calendar".equals(type)){
//				sb.Append("\r\n  editor_%1$s.bind('onpicked',function(event){$(this).parents('td:first').addClass('grid-dirty');$(this).parents('tr:first').attr('dirty',true);});",this.getBaseTag().getId());
//			}if("combobox".equals(type)){
//				sb.Append("\r\n  editor_%1$s.bind('onpicked',function(event){$(this).parents('td:first').addClass('grid-dirty');$(this).parents('tr:first').attr('dirty',true);});",this.getBaseTag().getId());
//			}else{
//				sb.Append("\r\n  editor_%1$s.bind('change',function(event){$(this).parents('td:first').addClass('grid-dirty');$(this).parents('tr:first').attr('dirty',true);});",this.getBaseTag().getId());
//			}			
//			sb.Append("$P.grid.editors['%1$s']=editor_%1$s;",this.getBaseTag().getId(),strSelect.toString());
//			sb.Append("editor_%1$s=null;",this.getBaseTag().getId());
			sb.Append("</script>");
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
